<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Utils\ModelOperatorTrait;
use Illuminate\Validation\Rule;

class IcController extends Controller
{
    use ModelOperatorTrait;

    protected function model(){
        return app(\App\Models\IC::class);
    }

    protected function getAttributes(){
        return [
            'print_code' => 'IC卡印刷编号',
            'ic_id' => 'IC卡号（0扇区0块）',
            'ic_status' => 'IC卡状态',
            'process_number' => 'IC卡对应的业务流程SALE销售发货',
            'current_process_node' => '当前业务节点',
            'next_process_node' => '下一业务节点',
            'vehicle_number' => '车牌号码',
            'driver_name' => '司机姓名',
            'driver_idcard' => '司机身份证号码',
            'driver_mobile_phone' => '司机手机号码',
            'update_datetime' => '更新时间',
            'remarks' => '备注'
        ];
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return formatJsonResponse($this->pageModel(request()->input()));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $rules = [
            'print_code' => 'required|unique:ic,print_code',
            'ic_id' => 'required|unique:ic,ic_id',
            'ic_status' => [
                'required',
                Rule::in(array_keys($this->model()::$IC_STATUS))
            ]
        ];

        if(!isset($request->ic_status)){
            $request->offsetSet('ic_status','VLAID');
        }

        $this->validate($rules);

        $this->storeModel($request->all());

        return formatJsonResponse();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        return formatJsonResponse($this->findModel($id));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $rules = [
            'print_code' => 'required|unique:ic,print_code',
            'ic_id' => 'required|unique:ic,ic_id',
            'ic_status' => [
                'required',
                Rule::in(array_keys($this->model()::$IC_STATUS))
            ]
        ];

        $this->validate($rules);

        $this->updateModel($id,$request->all(),null);

        return formatJsonResponse();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $this->deleteModel($id);

        return formatJsonResponse();
    }

    public function processNode(){
        return formatJsonResponse(itemOption($this->model()::$PROCESS_NODE));
    }

    public function icStatus(){
        return formatJsonResponse(itemOption($this->model()::$IC_STATUS));
    }
}
